package com.demo.cms.dao.impl;

import java.util.List;
import java.util.Map;

import com.demo.cms.dao.NaviDao;
import com.demo.cms.model.Navi;
import com.demo.core.cache.CacheKey;
import com.demo.core.cache.CacheWrapper;
import com.demo.core.dao.impl.base.PageCoreDaoSupport;
import com.demo.core.model.support.GeneralExecuteModel;
import com.demo.core.model.support.converter.AdjacencyListToChainedObjectConverter;

/**
 * 导航数据访问实现类
 * 
 * @author ryuu.kk
 * @since 2013-04-23 3:20 pm
 *
 */
public class NaviDaoImpl extends PageCoreDaoSupport<Navi, Long> implements NaviDao {

	/**
	 * 目录空间 
	 */
	protected final static String TABLE_SPACE = "naviSpace.";
	
	/**
	 * 模型表名
	 */
	protected final static String TABLE_NAME = "T_CMS_NAVI";
	
	/**
	 * 模型表主键ID
	 */
	protected final static String TABLE_PK_NAME = "ID";
	
	@Override
	protected String nameSpace() {
		return TABLE_SPACE;
	}

	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setPk(TABLE_PK_NAME);
		model.setTable(TABLE_NAME);
	}

	@Override
	public int enabled(Boolean enabled, String status, Map<String, Object> params) {
		cacheClear();
		Navi navi = new Navi();
		navi.setEnabled(enabled);
		navi.setStatus(status);
		params.put("navi", navi);
		return getSqlMapClientTemplate().update(nameSpace() + "updateEnabled", params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Navi> query(Map<String, Object> params) {
		CacheKey key = null;
		List<Navi> result = null;
		CacheWrapper<CacheKey, Object> cache = null;
		
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			key.update(nameSpace() + "queryTree");
			key.update(params.hashCode());
			result = (List<Navi>) cache.get(key);
		}
		if (result == null) {
			result = getSqlMapClientTemplate().queryForList(nameSpace() + "queryTree", params);
			if (isUseCache() && cache != null && result != null) {
				cache.put(key, result);
			}
		}
		return result;
	}

	@Override
	public Navi tree(Map<String, Object> params) {
		List<Navi> naviList = query(params);
		return AdjacencyListToChainedObjectConverter.conver(naviList);
	}
}
